/**
 * 
 */
package com.sad.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.sad.dao.RecordManager;
import com.sad.model.JsonObject;
import com.sad.model.Wish;
import com.sad.service.BaseService;
import com.sad.util.DateUtils;
import com.sad.util.G;

/**
 * @author zhaoqy
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class WishService extends BaseService<Map> {

	@Override
	public List<Map> findList(Map parms) {
		String pageid = StringUtils.defaultString((String) parms.get(G.K.pageid), "wish");

		JsonObject<Wish> jObject = (JsonObject<Wish>) RecordManager.loadRecord(pageid).get(parms);
		List<Map> wishList = new ArrayList<Map>();
		int totalcount = 0;
		if (jObject != null && jObject.getPage() != null) {
			totalcount = jObject.getPage().getTotalcount();
		}

		try {
			if (jObject != null && jObject.getInfo() != null) {
				List<Wish> wishs = jObject.getInfo();
				for (Wish w : wishs) {

					Map map = new HashMap();
					map.put("status", w.getStatus());
					map.put("orderid", w.getOrderId());
					// 预约编号
					map.put("wishid", w.getWishId());
					map.put("userid", w.getUserId());
					// 玩具编号
					map.put("toycode", w.getToyCode());
					if (w.getUser() != null) {
						// 用户名
						map.put("username", w.getUser().getUsername());
						map.put("address", w.getUser().getAddress());
						map.put("mobile", w.getUser().getMobile());
					} else {
						map.put("username", "");
						map.put("address", "");
						map.put("mobile", "");
					}
					if (w.getToy() != null) {
						// 主题？
						map.put("toytheme", w.getToy().getToyTheme());
						// 库存？
						map.put("toysetscount", w.getToy().getToysetsCount());
					} else {
						map.put("toytheme", "");
						map.put("toysetscount", "");
					}

					map.put("status", w.getStatus());
					if (w.getCreatetime() != null) {
						map.put("createtime",
								DateUtils.showTime(new Date(w.getCreatetime().getTime()), null));
					} else {
						map.put("createtime", "");
					}

					wishList.add(map);
				}
			}

		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}

		Map page = new HashMap();
		page.put("totalcount", totalcount);
		wishList.add(0, page);

		return wishList;
	}

	/* (non-Javadoc)
	 * @see com.sad.service.BaseService#create(java.util.Map)
	 */
	@Override
	public String create(Map<String, Object> parms) {
		String pageid = (String) parms.get(G.K.pageid);
		JsonObject jObject = (JsonObject) RecordManager.loadRecord(pageid).get(parms);
		return buildJson(jObject);
	}	
}
